<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('积分商城商品列表')"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
    <th:block th:include="include :: jasny-bootstrap-css"/>
    <style>
        .wrap {
            position: relative;
        }

        .sm-mark {
            position: absolute;
            top: 0;
            left: 0;
            margin: 0;
        }

        .xs-mark {
            position: absolute;
            top: 0;
            left: 0;
            margin: 0;
        }

        .sm-mark:before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            border-style: solid;
            border-width: 16px;
            z-index: 1;
            border-color: rgba(206, 3, 5, .7) transparent transparent rgba(206, 3, 5, .7);
        }

        .xs-mark:before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            border-style: solid;
            border-width: 8px;
            z-index: 1;
            border-color: rgba(206, 3, 5, .7) transparent transparent rgba(206, 3, 5, .7);
        }

        .img-sm-text {
            color: #FFF;
            display: inline-block;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
            font-size: 12px;
            text-transform: uppercase;
            width: 20px;
            text-align: center;
        }

        .img-xs-text {
            color: #FFF;
            display: inline-block;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
            font-size: 12px;
            text-transform: uppercase;
            width: 0;
            text-align: center;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="product-form">
                <div class="select-list">
                    <ul>
                        <li>
                            <p style="width: 80px">商品名称： </p>
                            <input type="text" name="productName" value=""/>
                        </li>
                        <li>
                            <p style="width: 80px">商品分类： </p>
                            <input type="hidden" name="categoryIdStr"/>
                            <input type="text" onclick="selectCategoryTree()" id="categoryName"
                                   placeholder="全部"/>
                        </li>
                        <li>
                            <p style="width: 80px">商品状态： </p>
                            <select name="isEnable" th:with="type=${@dict.getType('product_status')}">
                                <option value="">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <!--<li class="select-time">-->
                            <!--<p style="width: 80px">创建时间： </p>-->
                            <!--<input type="text" class="time-input" placeholder="开始时间"-->
                                   <!--name="params[beginTime]"/>-->
                            <!--<span>-</span>-->
                            <!--<input type="text" class="time-input" placeholder="结束时间"-->
                                   <!--name="params[endTime]"/>-->
                        <!--</li>-->
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="formReset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.addTab()" shiro:hasPermission="back:shopProduct:creditAdd">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary multiple disabled" onclick="multiEdit(1,1)">
                <i class="fa fa-check"></i> 批量上架
            </a>
            <a class="btn btn-danger multiple disabled" onclick="multiEdit(1,0)">
                <i class="fa fa-ban"></i> 批量下架
            </a>
            <!--<a class="btn btn-warning multiple disabled" onclick="selectMember()" shiro:hasPermission="back:shopProduct:sendToMember">-->
                <!--<i class="fa fa-gift"></i> 赠送礼品-->
            <!--</a>-->
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<th:block th:include="include :: jasny-bootstrap-js"/>
<script th:inline="javascript">
    const addFlag = [[${@permission.hasPermi('back:shopProduct:creditAdd')}]];
    const editFlag = [[${@permission.hasPermi('back:shopProduct:creditEdit')}]];
    const removeFlag = [[${@permission.hasPermi('back:shopProduct:creditRemove')}]];
    const productStatusDict = [[${@dict.getType('product_status')}]];
    const productTypeDict = [[${@dict.getType('product_type')}]];
    const isEnableDict = [[${@dict.getType('is_enable')}]];
    const prefix = ctx + "back/shopProduct";

    $(function () {
        var options = {
            url: prefix + "/searchCreditList",
            height: 600,
            createUrl: prefix + "/creditAdd",
            updateUrl: prefix + "/creditEdit/{id}",
            removeUrl: prefix + "/creditRemove",
            sortName: "createTime desc,orderNum",
            sortOrder: "asc",
            modalName: "积分商城商品",
            paginationShowPageGo: true,
            clickToSelect: true,
            onEditableSave: onEditableSave,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '站点商品ID',
                    visible:false
                },
                {
                    field: 'productCategory',
                    title: '商品分类',
                    sortable: true
                },
                {
                    field: 'productType',
                    title: '商品类型',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(productTypeDict, value);
                    }
                },
                {
                    field: 'productName',
                    title: '商品名称',
                    sortable: true
                },
                {
                    field: 'coverUrlThumb',
                    title: '封面图',
                    formatter: function (value, row, index) {
                        var imgHtml = "<img class='img-circle img-sm' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s' data-src='%s'/>";
                        return $.common.sprintf(imgHtml, 400, 'auto', 'self', value, row.coverUrl);
                    }
                },
                {
                    field: 'productPrice',
                    title: '兑换积分',
                    sortable: true,
                    editable: {
                        type: 'text',
                        title: '兑换积分',
                        emptytext: "【兑换积分】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【兑换积分】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【兑换积分】必须是数字';
                            }
                            if (!(/(^[1-9]\d*$)/.test(v))) {
                                return '请输入正确的【兑换积分】';
                            }
                        }
                    }
                },
                {
                    field: 'sale',
                    title: '已兑换数量',
                    sortable: true
                },
                {
                    field: 'stock',
                    title: '库存',
                    sortable: true,
                    editable: {
                        type: 'text',
                        title: '库存',
                        emptytext: "【库存】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【库存】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【库存】必须是数字';
                            }
                            if (!(/(^[0-9]\d*$)/.test(v))) {
                                return '请输入正确的【库存】';
                            }
                        }
                    }
                },
                {
                    field: 'orderNum',
                    title: '显示排序',
                    sortable: true,
                    editable: {
                        type: 'text',
                        title: '显示排序',
                        emptytext: "【显示排序】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【显示排序】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【显示排序】必须是数字';
                            }
                            if (!(/(^[0-9]\d*$)/.test(v))) {
                                return '请输入正确的【显示排序】';
                            }
                        }
                    }
                },
                {
                    field: 'isEnable',
                    title: '状态',
                    sortable: true,
                    editable: {
                        type: 'select',
                        title: '状态',
                        source: [{
                            value: 1,
                            text: "在售"
                        }, {
                            value: 0,
                            text: "停售"
                        }]
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.id + '\')"><i class="fa fa-edit">编辑</i></a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-trash"></i>删除</a>');
                        return actions.join('');
                    }
                }
                ]
        };
        $.table.init(options);
    });

    function onEditableSave(field, row,oldValue,$el) {
        let updateData = {};
        updateData[field] = row[field];
        updateData.id = row.id;
        $.ajax({
            type: 'post',
            url: ctx + "back/shopProduct/creditEdit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("修改成功");
                } else {
                    $.modal.alertError(result.msg);
                }
                $.table.refresh();
            }
        });
    }

    function multiEdit(type, val) {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        let updateData = {
            ids: rows.join()
        };
        switch (parseInt(type)) {
            case 1:
                //批量上架/下架
                updateData.isEnable = val;
                break;
            default:
                $.modal.alertWarning("操作失败！");
                return;
        }
        $.ajax({
            type: 'post',
            url: ctx + "back/shopProduct/creditEdit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("操作成功！");
                } else {
                    $.modal.alertError(result.msg);
                }
                $.table.refresh();
            }
        });
    }

    function formReset() {
        $('input[name="categoryIdStr"]').val("");
        $.form.reset();
    }

    /* 商品分类筛选*/
    function selectCategoryTree() {
        var categoryIdStr = $("input[name='categoryIdStr']").val();
        categoryIdStr = $.common.isEmpty(categoryIdStr) ? "0" : $("input[name='categoryIdStr']").val();
        var url = ctx + "back/productCategory/selectTree/" + categoryIdStr;
        var options = {
            title: '商品分类筛选',
            width: "380",
            url: url,
            callBack: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                if (parseInt(body.find('#treeId').val()) !== 0) {
                    $('input[name="categoryIdStr"]').val(body.find('#treeId').val());
                } else {
                    $('input[name="categoryIdStr"]').val("");
                }
                $("#categoryName").val(body.find('#treeName').val());
                // var tree = layero.find("iframe")[0].contentWindow.$._tree;
                // let checkedNodes = tree.getCheckedNodes(true);
                // let categoryIdStr = "";
                // let categoryNameStr = "";
                // for (let i = 0; i < checkedNodes.length; i++) {
                //     categoryIdStr += checkedNodes[i].id + ",";
                //     categoryNameStr += checkedNodes[i].name + ",";
                // }
                // $('input[name="categoryIdStr"]').val(categoryIdStr.substring(0,categoryIdStr.length-1));
                // $('#categoryName').val(categoryNameStr.substring(0,categoryNameStr.length-1));
                layer.close(index);
                $.table.search();
            }
        };
        $.modal.openOptions(options);
    }

    /**
     * 赠送会员礼品
     */
    function selectMember() {
        var rows = $("#bootstrap-table").bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一份积分商品赠送");
            return;
        }
        var isAbleSend = true;
        $.each(rows, function(i, d) {
            if(1 != d.isEnable){
                $.modal.alertWarning("积分商品【"+d.productName+"】不是在售状态");
                isAbleSend=false;
                return isAbleSend;
            }
            if(0 == d.stock){
                $.modal.alertWarning("积分商品【"+d.productName+"】库存不足");
                isAbleSend=false;
                return isAbleSend;
            }
        });
        if(!isAbleSend){
            return;
        }
        var url = prefix + "/selectMember/" + $.table.selectFirstColumns().join();
        $.modal.open("选择受赠礼品的会员", url, 1000);
    }
</script>
</body>
</html>